package com.hesicare.health.worker;

import com.hesicare.adapter.entity.HisPrescriptionOther;
import com.hesicare.enums.adviceEnum;
import com.hesicare.health.entity.HisAdviceMidinfo;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.entity.SysUser;
import com.hesicare.health.service.IHisAdviceMidinfoService;
import com.hesicare.health.service.ISysPatientService;
import com.hesicare.health.service.ISysUserService;
import com.hesicare.untils.StringUtil.StringUtil;
import com.hesicare.untils.dateUtil.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import static com.hesicare.enums.adviceEnum.CREATEBY;
import static com.hesicare.enums.adviceEnum.ENABLE;
import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

/**
 * @author 55097
 */
@Component
public class DoAdvice {

    @Autowired
    private IHisAdviceMidinfoService ihisAdviceMidinfoService;

    @Autowired
    private ISysUserService ISysUserService;

    @Autowired
    private ISysPatientService ISysPatientService;

    protected final static Logger log = LoggerFactory.getLogger(DoAdvice.class);

    public synchronized boolean doAdvice(HisPrescriptionOther hisPrescriptionOther) {
        /**
         * 查询医嘱对应的患者是否在后台存在
         * */
        SysPatient sysPatient = ISysPatientService.selectSysPatientByHospitalizedNum(hisPrescriptionOther.getCardNumber());
        if (sysPatient == null) {
            return false;
        }
        /**
         * 判断同步信息是否有效
         * */
        if (!checkAdvice(hisPrescriptionOther)) {
            return false;
        }
        /**
         * 通过医嘱编号查询医嘱是否存在
         * */
        HisAdviceMidinfo hisAdviceMidinfo = ihisAdviceMidinfoService.getHisAdviceMidinfoById(hisPrescriptionOther.getPrescriptionId());
        if (hisAdviceMidinfo == null && ENABLE.getType().equals(hisPrescriptionOther.getPrescriptionStatus())) {
            hisAdviceMidinfo = new HisAdviceMidinfo();
            try {
                /**
                 * 转换对象
                 * */
                this.transform(hisPrescriptionOther, hisAdviceMidinfo);
                this.createAdviceInfo(hisPrescriptionOther, hisAdviceMidinfo, sysPatient);
                ihisAdviceMidinfoService.insertHisAdviceMidinfo(hisAdviceMidinfo);
                log.info("新增医嘱" + hisAdviceMidinfo.toString());
            } catch (Exception e) {
                log.error("新增医嘱错误" , errorTrackSpace(e));
            }
            return true;
        } else if (hisAdviceMidinfo != null) {
            /**
             * 判断医嘱信息是否有更新
             * */
            if (!isChange(hisPrescriptionOther, hisAdviceMidinfo)) {
                return false;
            }
            try {
                transform(hisPrescriptionOther, hisAdviceMidinfo);
                updateAdviceInfo(hisAdviceMidinfo);
                ihisAdviceMidinfoService.updateHisAdviceMidinfo(hisAdviceMidinfo);
                log.info("医嘱更新" + hisAdviceMidinfo.toString());
            } catch (Exception e) {
                log.error("更新医嘱错误", errorTrackSpace(e));
            }
            return true;
        }
        return false;
    }

    public boolean checkAdvice(HisPrescriptionOther hisPrescriptionOther) {
        if (StringUtil.isBlank(hisPrescriptionOther.getPrescriptionId())) {
            return false;
        }
        if (StringUtil.isBlank(hisPrescriptionOther.getFrequency())) {
            return false;
        }
        if (StringUtil.isBlank(hisPrescriptionOther.getPrescriptionStatus())) {
            return false;
        }
        if (StringUtil.isBlank(hisPrescriptionOther.getPrescriptionName())) {
            return false;
        }
        if (StringUtil.isBlank(hisPrescriptionOther.getPatientId())) {
            return false;
        }
        if (hisPrescriptionOther.getStartTime() == null) {
            return false;
        }
        return true;
    }

    public void transform(HisPrescriptionOther hisPrescriptionOther, HisAdviceMidinfo hisAdviceMidinfo) {
        hisAdviceMidinfo.setPrescriptionId(hisPrescriptionOther.getPrescriptionId());
        hisAdviceMidinfo.setHisPatientId(hisPrescriptionOther.getPatientId());
        hisAdviceMidinfo.setHospitalizedNum(hisPrescriptionOther.getCardNumber());
        hisAdviceMidinfo.setPerFrequency(hisPrescriptionOther.getPerFrequency());
        hisAdviceMidinfo.setPrescriptionType(hisPrescriptionOther.getFrequency());
        hisAdviceMidinfo.setEndTime(hisPrescriptionOther.getEndTime());
        hisAdviceMidinfo.setPrescriptionNotes(hisPrescriptionOther.getPrescriptionNotes());
        hisAdviceMidinfo.setPrescriptionNo(hisPrescriptionOther.getPrescriptionNo());
        hisAdviceMidinfo.setPrescriptionName(hisPrescriptionOther.getPrescriptionName());
        hisAdviceMidinfo.setMeasureDataType(hisPrescriptionOther.getPrescriptionType());
        hisAdviceMidinfo.setTask(hisPrescriptionOther.getTask());
        hisAdviceMidinfo.setTimes(hisPrescriptionOther.getTimes());
        hisAdviceMidinfo.setType(hisPrescriptionOther.getType());
        hisAdviceMidinfo.setRemark(hisPrescriptionOther.getRemark());
        hisAdviceMidinfo.setPrescriptionStatus(hisPrescriptionOther.getPrescriptionStatus());
        hisAdviceMidinfo.setOperatorStatus(ENABLE.getType());
        /**
         * his为拆分医嘱type为0，his已拆分医嘱type为1
         * */
        hisAdviceMidinfo.setType(ENABLE.getType());
        hisAdviceMidinfo.setExt1(hisPrescriptionOther.getExt1());
    }

    public void createAdviceInfo(HisPrescriptionOther hisPrescriptionOther, HisAdviceMidinfo hisAdviceMidinfo, SysPatient sysPatient) {
        hisAdviceMidinfo.setCreatDate(new Date());
        /**
         * 通过his_user_id 查询员工信息
         * */
        SysUser sysUser = ISysUserService.getUserByHisUserId(hisPrescriptionOther.getDoctorId());
        if (sysUser != null) {
            hisAdviceMidinfo.setLoginName(sysUser.getLoginName());
            hisAdviceMidinfo.setDoctorId(sysUser.getUserId().toString());
            hisAdviceMidinfo.setDoctorName(sysUser.getUserName());
        } else {
            hisAdviceMidinfo.setLoginName(CREATEBY.getType());
            hisAdviceMidinfo.setDoctorId(CREATEBY.getType());
            hisAdviceMidinfo.setDoctorName(CREATEBY.getType());
        }
        hisAdviceMidinfo.setCreateBy(CREATEBY.getType());
        hisAdviceMidinfo.setCreateTime(new Date());
        Date startTime = hisPrescriptionOther.getStartTime();
        if (adviceEnum.ONCE.getType().equals(hisPrescriptionOther.getFrequency())) {
            if (startTime.before(hisAdviceMidinfo.getCreatDate())) {
                startTime = DateUtil.addMinutes(hisAdviceMidinfo.getCreatDate(), 6);
            }
        }
        hisAdviceMidinfo.setStartTime(startTime);
        hisAdviceMidinfo.setPatientId(sysPatient.getPatientId());
    }

    public void updateAdviceInfo(HisAdviceMidinfo hisAdviceMidinfo) {
        hisAdviceMidinfo.setUpdateBy(CREATEBY.getType());
        hisAdviceMidinfo.setUpdateTime(new Date());
    }

    public boolean isChange(HisPrescriptionOther hisPrescriptionOther, HisAdviceMidinfo hisAdviceMidinfo) {
        if (!hisPrescriptionOther.getPrescriptionName().equals(hisAdviceMidinfo.getPrescriptionName())) {
            return true;
        }
        if (!hisPrescriptionOther.getPrescriptionStatus().equals(hisAdviceMidinfo.getPrescriptionStatus())) {
            return true;
        }
        if (hisPrescriptionOther.getExt1() != null && hisPrescriptionOther.getExt1() != hisAdviceMidinfo.getExt1()) {
            return true;
        }
        return false;
    }
}
